package com.ruoyi.project.task.mission.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.person.info.domain.PersonInfo;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.task.mission.domain.TaskMission;
import com.ruoyi.project.task.mission.domain.TaskMissionVo;
import com.ruoyi.project.task.mission.mapper.TaskMissionMapper;
import com.ruoyi.project.task.postpone.domain.TaskPostpone;
import com.ruoyi.project.task.postpone.mapper.TaskPostponeMapper;
import com.ruoyi.project.task.receive.domain.TaskReceive;
import com.ruoyi.project.task.receive.mapper.TaskReceiveMapper;
import com.ruoyi.project.task.transfer.mapper.TaskTransferMapper;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Pathosis
 * @date 2020-09-17 11:09
 * @description: 任务创建的接口实现类
 */
@Service
public class TaskMissionServiceImpl implements ITaskMissionService{
    @Autowired
    private TaskMissionMapper tmm;

    @Autowired
    private TaskReceiveMapper trm;

    @Autowired
    private TaskPostponeMapper tpm;

    @Autowired
    private TaskTransferMapper ttm;

    @Override
    public List<TaskMissionVo> selectTaskMissionList(TaskMissionVo taskMissionVo) {
        return tmm.selectTaskMissionList(taskMissionVo);
    }

    @Override
    public String checkMissionTitleUnique(TaskMission tmv) {
        String s = tmm.selectTaskMissionByTitle(tmv.getMissionTitle());
        if(s != null && !"".equals(s)){
            return UserConstants.MISSION_TITLE_NOT_UNIQUE;
        }
        return UserConstants.MISSION_TITLE_UNIQUE;
    }

    @Override
    public String checkInfoNameUnique(TaskMissionVo tmv) {
        if(UserConstants.PERSON_NOT_ISREAL.equals(tmv.getInfoId())){
            return UserConstants.INFO_NAME_NOT_UNIQUE;
        }else{
            PersonInfo personInfo = tmm.checkInfoNameUnique(tmv);
            if(personInfo != null && !"".equals(personInfo)){
                //查到了返回0，说明员工存在，可以使用
                return UserConstants.INFO_NAME_NOT_UNIQUE;
            }
        }
        return UserConstants.INFO_NAME_UNIQUE;
    }

    @Override
    public int insertTaskMissionVo(TaskMission taskMission , String endTime) {
        taskMission.setEndTime(StrToDate(endTime));
        taskMission.setCreateBy(ShiroUtils.getLoginName());
        if(UserConstants.PERSON_NOT_ISREAL.equals(taskMission.getInfoId())){
            taskMission.setIsReceive(UserConstants.MISSION_ISRECEIVE_F);
        }else{
            taskMission.setIsReceive(UserConstants.MISSION_ISRECEIVE_T);
        }
        int num = tmm.insertTaskMissionVo(taskMission);
            TaskReceive tr = new TaskReceive();
            tr.setInfoId(taskMission.getInfoId());
            tr.setMissionId(taskMission.getMissionId());
            trm.insertTaskReceIve(tr);
        return num;
    }

    @Override
    public List<PersonInfo> selectAllPerson() {
         List<PersonInfo> list = tmm.selectAllPerson();
        return list;
    }

    @Override
    public int deleteTaskMissionByIds(String ids) {
        // 任务id
        Long[] missionIds = Convert.toLongArray(ids);

        //根据任务id删除TaskPostpone对象
        tpm.deleteTaskPostponeByMission(missionIds);
        //根据任务id删除TaskTransfer对象
        ttm.deleteTaskTransferByMissionId(missionIds);

        return tmm.deleteTaskMissionByIds(missionIds);
    }

    @Override
    public TaskMission selectTaskMissionById(Long missionId) {
        return tmm.selectTaskMissionById(missionId);
    }

    @Override
    public int updateTaskMission(TaskMission taskMission) {
        taskMission.setUpdateBy(ShiroUtils.getLoginName());
        if(UserConstants.PERSON_NOT_ISREAL.equals(taskMission.getInfoId())){
            taskMission.setIsReceive(UserConstants.MISSION_ISRECEIVE_F);
        }else{
            taskMission.setIsReceive(UserConstants.MISSION_ISRECEIVE_T);
        }
        return tmm.updateTaskMission(taskMission);
    }

    @Override
    public TaskMissionVo selectTaskMissionVoById(Long missionId) {
        return tmm.selectTaskMissionVoById(missionId);
    }


    /**
     * 字符串转换成时间
     * @param endTime
     * @return
     */
    private Date StrToDate(String endTime){
        SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date format = null;
        try {
            format = dff.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  format;
    }
}
